package org.example.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

/**
 * @author objcat
 * @version 1.0
 * @description CustomRealm
 * @date 2022/10/14 23:54
 */
public class CustomRealm5 extends AuthorizingRealm {
    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 获取用户名 该操作会获取到 UsernamePasswordToken 中的用户名 objcat
        String principal = (String) authenticationToken.getPrincipal();
        if ("objcat".equals(principal)) {
            return new SimpleAuthenticationInfo("objcat", "34aa1351e318ef7a99ddd6c690b10553", ByteSource.Util.bytes("666"), this.getName());
        }
        return null;
    }
}
